package org.dentalcard.ui.schedule;

import org.dentalcard.entity.DecUser;
import org.dentalcard.entity.Shift;
import org.dentalcard.entity.WorkDay;
import org.dentalcard.service.PersistenceManager;
import org.dentalcard.service.impl.ScheduleDaoImpl;
import org.dentalcard.ui.api.tools.ComponentTableEditor;
import org.dentalcard.ui.api.tools.ComponentTableRenderer;
import org.dentalcard.ui.api.tools.DecDialog;
import org.dentalcard.ui.i18n.I18nManager;
import org.dentalcard.ui.main.MainFrameBuilder;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import java.util.List;

/**
 * @user: Vladimir Chernishov
 * @date: 25.02.12
 * @time: 16:59
 */
public class WorkDayShiftManagement extends DecDialog {

    private final JPanel main;
    private final DecUser user;
    private JTable weekTable;
    private Vector<JComboBox> odd = new Vector<JComboBox>();
    private Vector<JComboBox> even = new Vector<JComboBox>();
    private Vector<JComboBox> always = new Vector<JComboBox>();

    public WorkDayShiftManagement(Frame owner) {
        super(owner, I18nManager.getRes("work.day.shift.management.screen"), false);
        this.main = new JPanel(new GridBagLayout());
        this.user = MainFrameBuilder.getUser();
        this.getContentPane().add(new JScrollPane(main));
        setPreferredSize(new Dimension(700, 400));
        setResizable(true);
        init();
        pack();
        center(owner);
    }

    private void init() {
        int rowN = 0;


        main.add(this.getWeekTable().getTableHeader(), new GridBagConstraints(0, rowN++, 1 ,1, 1, 0, GridBagConstraints.FIRST_LINE_START,
                GridBagConstraints.HORIZONTAL, new Insets(5, 5, 0, 5), 0, 0));
        main.add(this.getWeekTable(), new GridBagConstraints(0, rowN++, 1 ,1, 1, 0, GridBagConstraints.FIRST_LINE_START,
                GridBagConstraints.HORIZONTAL, new Insets(5, 5, 0, 5), 0, 0));


        main.add(createOkButton(), new GridBagConstraints(0, rowN++, 1 ,1, 0, 0, GridBagConstraints.CENTER,
                GridBagConstraints.HORIZONTAL, new Insets(0, 5, 0, 5), 0, 0));
    }

    private JButton createOkButton() {
        JButton ok = new JButton("OK");
        ok.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Calendar calendar = GregorianCalendar.getInstance();
                calendar.set(Calendar.HOUR_OF_DAY, 12);
                calendar.set(Calendar.MINUTE, 0);
                calendar.set(Calendar.SECOND, 0);
                calendar.set(Calendar.MILLISECOND, 0);
                int maxDay = calendar.getMaximum(Calendar.DAY_OF_YEAR);
                for(int i = 1; i < maxDay; i++) {
                    calendar.set(Calendar.DAY_OF_YEAR, i);
                    int day;
                    if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
                        day = 6;
                    } else {
                        day = calendar.get(Calendar.DAY_OF_WEEK) - 2;
                    }

                    Shift shift;
                    if (always.get(day).getSelectedItem() != null) {
                        shift = (Shift) always.get(day).getSelectedItem();
                    } else if (calendar.get(Calendar.DAY_OF_MONTH) % 2 == 1) {
                        shift = (Shift) odd.get(day).getSelectedItem();
                    } else {
                        shift = (Shift) even.get(day).getSelectedItem();
                    }

                    if(shift == null || shift == Shift.DAY_OFF){
                        continue;
                    }

                    WorkDay workDay = new WorkDay();
                    workDay.setDay(calendar.getTime());
                    workDay.setShift(shift);
                    workDay.setUserId(user.getId());
                    PersistenceManager.saveOrUpdate(workDay);
                }
                WorkDayShiftManagement.this.dispose();
            }
        });
        return ok;
    }

    public JTable getWeekTable() {
        if(weekTable == null){
            final DefaultTableModel model = new DefaultTableModel(0, 7);
            weekTable = new JTable(model);
            model.setColumnIdentifiers(new Object[]{I18nManager.getRes("monday"), I18nManager.getRes("tuesday"),
                    I18nManager.getRes("wednesday"), I18nManager.getRes("thursday"), I18nManager.getRes("friday"),
                    I18nManager.getRes("saturday"), I18nManager.getRes("sunday")});

            ComponentTableRenderer renderer = new ComponentTableRenderer();
            ComponentTableEditor editor = new ComponentTableEditor();

            ScheduleDaoImpl dao = new ScheduleDaoImpl();
            List<Shift> shiftList = new ArrayList<Shift>();
            shiftList.add(null);
            shiftList.add(Shift.DAY_OFF);
            shiftList.addAll(dao.getShiftList(user));


            for(int i = 0; i < 7; i++){
                final JComboBox evenCombo = new JComboBox(shiftList.toArray());
                final JComboBox oddCombo = new JComboBox(shiftList.toArray());
                final JComboBox alwaysCombo = new JComboBox(shiftList.toArray());
                evenCombo.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        if(evenCombo.getSelectedItem() != null){
                            alwaysCombo.setSelectedItem(null);
                            model.fireTableDataChanged();
                        }
                    }
                });
                oddCombo.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        if(oddCombo.getSelectedItem() != null){
                            alwaysCombo.setSelectedItem(null);
                            model.fireTableDataChanged();
                        }
                    }
                });
                alwaysCombo.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        if(alwaysCombo.getSelectedItem() != null){
                            evenCombo.setSelectedItem(null);
                            oddCombo.setSelectedItem(null);
                            model.fireTableDataChanged();
                        }
                    }
                });
                even.add(evenCombo);
                odd.add(oddCombo);
                always.add(alwaysCombo);
                weekTable.setDefaultRenderer(Object.class, renderer);
                weekTable.setDefaultEditor(Object.class, editor);
            }

            model.addRow(new Object[]{I18nManager.getRes("odd"), I18nManager.getRes("odd"),
                    I18nManager.getRes("odd"), I18nManager.getRes("odd"), I18nManager.getRes("odd"),
                    I18nManager.getRes("odd"), I18nManager.getRes("odd")});
            model.addRow(odd);
            model.addRow(new Object[]{I18nManager.getRes("even"), I18nManager.getRes("even"),
                    I18nManager.getRes("even"), I18nManager.getRes("even"), I18nManager.getRes("even"),
                    I18nManager.getRes("even"), I18nManager.getRes("even")});
            model.addRow(even);
            model.addRow(new Object[]{I18nManager.getRes("always"), I18nManager.getRes("always"),
                    I18nManager.getRes("always"), I18nManager.getRes("always"), I18nManager.getRes("always"),
                    I18nManager.getRes("always"), I18nManager.getRes("always")});
            model.addRow(always);

            weekTable.setRowSelectionAllowed(false);
            weekTable.getTableHeader().setReorderingAllowed(false);

        }
        return weekTable;
    }
}
